﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace CreditAgricole.Framework
{
    public static class CALoggingHelper
    {        
        private const string SOURCE = "Crédit Agricole";
        private const string LOG_NAME = "Crédit Agricole";
        
        /// <summary>
        /// Add an information event to the Windows EventLog
        /// </summary>
        /// <param name="message">Message of the event</param>
        private static void AddEntry(string message)
        {
            AddEntry(message, EventLogEntryType.Information);
        }

        /// <summary>
        /// Add an event of the specified type to the Windows EventLog
        /// </summary>
        /// <param name="message">Message of the event</param>
        /// <param name="entryType">Type of entry</param>
        private static void AddEntry(string message, EventLogEntryType entryType)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (!EventLog.SourceExists(SOURCE)) EventLog.CreateEventSource(SOURCE, LOG_NAME);

                EventLog.WriteEntry(SOURCE, message, entryType);
            });            
        }

        /// <summary>
        /// Add an information event to the Crédit Agricole eventlog
        /// </summary>
        /// <param name="message">Message of the event</param>                        
        public static void AddInformationEntry(string message)
        {
            AddEntry(message);
        }

        /// <summary>
        /// Add a warning event to the Crédit Agricole eventlog
        /// </summary>
        /// <param name="message">Message of the event</param>                        
        public static void AddWarningEntry(string message)
        {
            AddEntry(message, EventLogEntryType.Warning);
        }

        /// <summary>
        /// Add an error event to the Crédit Agricole eventlog
        /// </summary>
        /// <param name="message">Message of the event</param>                        
        public static void AddErrorEntry(string message)
        {
            AddEntry(message, EventLogEntryType.Error);
        }
    }
}
